package com.corpgovernment.organization.service.impl;

import com.corpgovernment.api.organization.model.passenger.PassengerVo;
import com.corpgovernment.common.base.BaseRequestVO.UserInfo;
import com.corpgovernment.organization.constant.CommonConst;
import com.corpgovernment.organization.entity.db.MbFreshPassenger;
import com.corpgovernment.organization.vo.GetPassengerRequestVO;
import com.corpgovernment.organization.vo.GetPassengerResponseVO;
import com.corpgovernment.organization.vo.PassengerInfoVO;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.corpgovernment.common.enums.SystemType.zhongjin;

/**
 * @author jhxue
 * @create 2022-02-25-14:33
 */
@Service
@Slf4j
public class PassengerOwnService extends AbstractPassengerService {

	/**
	 * 查询出行人
	 *
	 * @param request
	 * @return
	 */
	@Override
	public GetPassengerResponseVO getPassenger(GetPassengerRequestVO request) {
		try {
			initElkLog();
			addElkInfoLog("进入因私出行获取可用出行人流程");
			UserInfo userInfo = request.getUserInfo();
			// 1.查询出行人信息
			List<PassengerVo> passengerList = this.getPassengerByOwn(request);
			// 2.完善出行人信息
			passengerList = this.packagePassengerList(passengerList, request);
			// 3.出行人排序
			passengerList = this.sort(passengerList, userInfo);
			// 4.封装返回
			return GetPassengerResponseVO.create(passengerList, true);
		} finally {
			log.info("PassengerService.getPassenger获取可用出行人{} request：{}{}{}{}", System.lineSeparator(), JsonUtils.toJsonString(request), System.lineSeparator(), System.lineSeparator(), this.getElkInfoLog());
			clearElkLog();
		}
	}

	/**
	 * 因私获取出行人
	 *
	 * @param requestVO
	 * @return
	 */
	private List<PassengerVo> getPassengerByOwn(GetPassengerRequestVO requestVO) {
		UserInfo userInfo = requestVO.getUserInfo();
		Integer pageNum = requestVO.getPage();
		String uid = userInfo.getUid();
		String orgId = userInfo.getOrgId();
		String query = requestVO.getQuery();
		List<MbFreshPassenger> freshPassengerList = new ArrayList<>();
		if (StringUtils.isBlank(query)) {
			freshPassengerList = freshPassengerLoader.pageByUidAndOrgId(uid, orgId, pageNum, CommonConst.PAGE_SIZE_30);
		} else {
			addElkInfoLog("查询条件：%s", query);
			String systemType = organizationApollo.getSystemType();
			List<Long> freshPassengerIds;
			if (Objects.equals(zhongjin.getCode(), systemType)) {
				addElkInfoLog("名称精确匹配或身份证号精确匹配");
				// 名称精确匹配或身份证号精确匹配
				freshPassengerIds = this.queryByNameOrCardNo(query, false);
			} else {
				addElkInfoLog("名称模糊查询");
				// 名称模糊查询
				freshPassengerIds = this.queryLikeName(query, false);
			}
			if (CollectionUtils.isNotEmpty(freshPassengerIds)) {
				freshPassengerList = freshPassengerLoader.pageByUidAndOrgIdAndFreshPassengerIds(freshPassengerIds, uid, orgId, pageNum, CommonConst.PAGE_SIZE_30);
			}
		}
		if (CollectionUtils.isEmpty(freshPassengerList)) {
			return new ArrayList<>();
		}
		addElkInfoLog("出行人信息：%s", JsonUtils.toJsonString(freshPassengerList));
		List<PassengerVo> passengerList = passengerConvert.convertFromFreshPassenger(freshPassengerList, orgId);
		PageInfo<MbFreshPassenger> pageInfo = new PageInfo<>(freshPassengerList);
		int totalPage = pageInfo.getPages();
		boolean pageEnd = requestVO.getPage() == totalPage;
		passengerList.forEach(e -> e.setPageEnd(pageEnd));
		List<PassengerInfoVO> selectedList = requestVO.getSelectedList();
		if (CollectionUtils.isEmpty(selectedList)) {
			return passengerList;
		}
		// 将选中的员工排掉
		List<String> uidList = selectedList.stream().filter(p -> StringUtils.isNotBlank(p.getUid())).map(PassengerInfoVO::getUid).collect(Collectors.toList());
		List<String> noUidList = selectedList.stream().filter(p -> StringUtils.isNotBlank(p.getNoEmployeeId())).map(PassengerInfoVO::getNoEmployeeId).collect(Collectors.toList());
		return passengerList.stream().filter(e -> !uidList.contains(e.getUid()) && !noUidList.contains(e.getNoEmployeeId())).collect(Collectors.toList());
	}

	@Override
	public String getType() {
		return "OWN";
	}
}
